﻿using System;
using SLNGP.Common.Extensions;
using SLNGP.Core.Individuals;
using SLNGP.Core.Run;

namespace SLNGP.Core.Genes
{
    public class ConstantGene<T> : Gene
    {
        private object _constant;
        public ConstantGene(object constant)
        {
            _constant = constant;
        }

        public override  object Execute(IGeneticProgrammingRun data)
        {
            if (!_constant.Is(typeof(T)))
            {
                throw new GeneticProgrammingException(string.Format("Gene execution failure. {0} constant is not of type{1}", _constant, typeof(T).FullName));
            }

            return (T)_constant;
        }

        public override string GetStringDisplay()
        {
            return _constant.ToString();
        }

        public override Type GeneReturnType
        {
            get { return typeof (T); }
        }
    }
}